package com.szxy.stream;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Author:zwer
 * @Date:2020/4/16 10:31
 * @Description:com.szxy
 * @Version:1.0
 * 创建 Stream 流
 * 1. Arrays: T stream(int[] array)
 * 2. 集合对象.stream()
 * 3. Stream<T> of(T... values)
 * 5. Stream<T> generate(Supplier<T> s)
 * 5. iterate(final T seed, final UnaryOperator<T> f)
 **/
public class StreamTest {

    @Test
    public void test1() {
        int[] arr = {1, 3 ,5, 7, 9};
        IntStream stream = Arrays.stream(arr);
        stream.limit(1).forEach(System.out::println);

        String[] strArr = {"2", "Hello", "World", "Java","Java", "hao"};
        Stream<String> stream1 = Arrays.stream(strArr);
        stream1.distinct()
                .filter(ele -> !ele.equals("World"))
                .map(ele -> ele + "123")
                .forEach(System.out::println);
    }

    @Test
    public void test2() {
        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        Stream<String> stream = list.stream();
        stream.forEach(System.out::println);
        // 相较与上面的方式，下面这个方式更加简洁
        Stream<String> stream2 = Stream.of("Hello", "World", "Java", "Python");
        List<String> list2 = stream2.collect(Collectors.toList());
        list2.forEach(System.out::println);
    }

    @Test
    public void test3() {
        Stream<Integer> stream = Stream.of(1, 2, 4);
        stream.forEach(System.out::println);
    }

    @Test
    public void test4() {
        Stream<Double> stream = Stream.generate(Math::random);
        stream.forEach(System.out::println);
    }

    @Test
    public void test5() {
        // 创建 Stream 流
        Stream<Integer> stream = Stream.iterate(1, t -> t + 2);
        // 中间操作
        Stream<Integer> stream1 = stream.limit(20);
        // 终结操作
        stream1.forEach(System.out::println);
    }

}
